package com.quanquan.management.service;

import DTO.PageDTO;
import com.quanquan.management.configBeans.RabbitSender;
import com.quanquan.management.dto.SystemLogDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.entity.SystemLog;
import com.quanquan.management.repository.SystemLogRepository;
import com.quanquan.management.request.SystemLogRequestBody;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.StringUtil;

import javax.persistence.criteria.Predicate;
import java.util.*;

/**
 * 功能描述:
 *
 * @ClassName: SystemLogService
 * @Author: xiaoqianh丶
 * @Date: 2019-08-21 11:44
 * @Version: V1.0
 */
@Service
public class SystemLogService {

    @Autowired
    private RabbitSender rabbitSender;

    @Autowired
    private SystemLogRepository systemLogRepository;

    public void createSystemLog(String userId, String remark, String name) {

        SystemLog systemLog = new SystemLog();
        systemLog.setUserId(userId);
        systemLog.setRemark(remark);
        systemLog.setName(name);
        systemLog.setCreateDate(new Date());
        systemLogRepository.save(systemLog);
    }


    public void sendSystemLog(SysUser sysUser, String remark) {
        String userId = sysUser.getId();
        String username = sysUser.getUsername();
        //发送消息推送
        Map<String, Object> properties = new HashMap<>();
        properties.put("userId", userId);
        properties.put("remark", remark);
        properties.put("name", username);


        //将计算结果放到MQ队列去补充明细的余额
        String msg = "发消息推送系统日志";
        String exchange = "system.exchange";
        String routingKey = "system.message";
        rabbitSender.sendMessageToMQ(msg, properties, exchange, routingKey);
    }


    public PageDTO<SystemLogDTO> findAllLogs(SystemLogRequestBody requestBody) {

        Integer page = requestBody.getPage();
        Integer size = requestBody.getSize();
        String keyword = requestBody.getKeyword();
        String from = requestBody.getFromDate();
        String to = requestBody.getToDate();

        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Pageable pageable = PageRequest.of(page, size,sort);
        SystemLog systemLog = new SystemLog();

        Page<SystemLog> all = systemLogRepository.findAll((Specification<SystemLog>) (root, query, criteriaBuilder) -> {

            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotEmpty(keyword)) {
                Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + keyword + "%");
                Predicate p2 = criteriaBuilder.or(criteriaBuilder.like(root.get("remark").as(String.class), "%" + keyword + "%"), p1);
                predicates.add(p2);
            }


            if (StringUtil.isNotEmpty(from)) {
                Date startOfDay = DateUtils.getStartOfDay(DateUtils.strToDate(from, DateUtils.DATE));

                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createDate").as(Date.class), startOfDay));
            }
            if (StringUtil.isNotEmpty(to)) {
                Date endOfDay = DateUtils.getEndOfDay(DateUtils.strToDate(to, DateUtils.DATE));
                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("createDate").as(Date.class), endOfDay));
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }, pageable);

        List<SystemLogDTO> allDto = new ArrayList<>();
        for (SystemLog systemLog1 : all) {
            SystemLogDTO systemLogDTO = new SystemLogDTO();
            //通过映射关系来赋值 //TODO 断点看一下
            BeanUtils.copyProperties(systemLog1, systemLogDTO);
            allDto.add(systemLogDTO);
        }

        PageDTO<SystemLogDTO> objectPageDTO = new PageDTO<>();
        objectPageDTO.setList(allDto);
        objectPageDTO.setPage(page);
        objectPageDTO.setPageSize(size);
        objectPageDTO.setTotalPage(all.getTotalPages());
        objectPageDTO.setTotal(all.getTotalElements());
        objectPageDTO.setFromDate(from);
        objectPageDTO.setToDate(to);
        objectPageDTO.setKeyword(keyword);
        return objectPageDTO;
    }
}
